package com.action.app_center;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;



import org.apache.struts2.ServletActionContext;
import org.aspectj.util.FileUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import com.enetiy.Area;
import com.enetiy.Feedback;
import com.enetiy.Userimg;
import com.enetiy.Userinfo;
import com.enetiy.Users;
import com.enetiy.Version;
import com.service.AreaService;
import com.service.FeedbackService;
import com.service.LoginService;
import com.service.UserimgService;
import com.service.VersionService;
import com.util.DataUtil;
import com.util.FileNameUtil;
import com.util.IPUtil;
import com.util.Log4gUtil;
import com.util.RandomUtil;
import com.util.Verify;

public class MyCenterAction {
	private LoginService loginService;
	private UserimgService userimgService;
	private FeedbackService feedbackService;
	private AreaService areaService;
	private VersionService versionService;
	private String params;
	
	private int userid;//用户ID
	private String ver;//app项目版本
	private int vertype;
	private int imgid;
	private String token;
	private File file;//文件
	private String fileFileName;
	
	private String feedbackContent;//反馈内容
	private String nickname;
	private int siteAreaid;
	private String pwd;
	private int appType;
	
	/**
	 * 
	 * 上传头像  >Nf022
	 */
	public void uploadHead(){
		Log4gUtil.getLog().info("uploadHead-进入-userid:"+userid+"-token:"+token);
		HttpServletResponse response = ServletActionContext.getResponse();
		ServletContext appli = ServletActionContext.getRequest().getSession().getServletContext();
		Object attribute = appli.getAttribute(userid+"");
		String tok=null;
		System.out.println(userid);
		JSONObject j = new JSONObject();
		PrintWriter out = null;
		File fi= null;//上传的文件
		try {
			out = response.getWriter();
			if(attribute!=null&&(tok=attribute.toString())!=null&&token.equals(tok)){
				System.out.println("头像wenjia：-----"+file);
				if(file!=null){
					//文件存储
					String path=IPUtil.getFilePath() +"/img/uersImg/headImg/";
					fi =new File(path);
					String fileName = FileNameUtil.imageupload(fileFileName, FileNameUtil.fileName());//文件名
					path = path+fileName;//文件路径
					Log4gUtil.getLog().info("uploadHead--userid:"+userid+"-文件名:"+fileName);
					System.out.println("路径----:"+path);
					File newfile = new File(fi,fileName);
					FileUtil.copyFile(file, newfile);//复制文件
					//文件名数据库存储
					Users u = loginService.selectUserId(userid);//id为用户Users.id
					if(u.getHeadImg()!=null&&!u.getHeadImg().equals("")&&!u.getHeadImg().contains("MoRenHead")){
						File fi1 = new File(fi,u.getHeadImg());
						fi1.delete();
					}
					u.setHeadImg(fileName);
					loginService.updateUser(u);//修改文件名成功
					j.put("code", 1);
					j.put("headurl",IPUtil.getIp()+"/img/uersImg/headImg/"+u.getHeadImg());
					j.put("msg", "修改头像成功");
				}else{
					j.put("code", -1);
					j.put("msg", "异常");
				}
			}else{
				j.put("code", -2);
				j.put("msg", "token验证未通过");
			}
				out.print(j);
		} catch (Exception e) {
			if(fi.exists()){
				fi.delete();//有异常删除文件
			}
			j.put("code", -1);
			j.put("msg", "异常");
			out.print(j);
			e.printStackTrace();
		}
		Log4gUtil.getLog().info("uploadHead--userid:"+userid+"-输出-:"+j.toString());
	}
	
	
	/**
	 * 
	 * 上传图片  >Nf021
	 */
	public void uploadPictures(){
		Log4gUtil.getLog().info("uploadPictures-进入-userid:"+userid+"-token:"+token);
		HttpServletResponse response = ServletActionContext.getResponse();
		ServletContext appli = ServletActionContext.getRequest().getSession().getServletContext();
		String tok = appli.getAttribute(userid+"").toString();
		
		JSONObject j = new JSONObject();
		PrintWriter out = null;
		File fi= null;
		try {
			out = response.getWriter();
			if(token.equals(tok)){ 
				if(file!=null&&userid!=0){
					System.out.println(file);
					String path = IPUtil.getFilePath()+"/img/uersImg/picturesImg";
					//文件存储
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
					String fileName = sdf.format(new Date())+RandomUtil.getRandom(10);//文件名
					fileName=fileName+".png";
					fi= new File(path,fileName);
					fi.createNewFile();//创建空文件
					FileUtil.copyFile(file, fi);//复制文件
					//文件名数据库存储
					Users u = loginService.selectUserId(userid);//id为用户Users.id
					Userimg ui = new Userimg();
					ui.setImgName(fileName);
					ui.setImgTime(new Date());
					ui.setUsers(u);
					userimgService.insetrUserImg(ui);//修改文件名成功
					j.put("code", 1);
					
				}else{
					j.put("code", -1);
				}
			}else{
				j.put("code", -2);
			}
			out.print(j);
		} catch (Exception e) {
			if(fi.exists()){
				fi.delete();//有异常删除文件
			}
			j.put("code", -1);
			out.print(j);
			e.printStackTrace();
		}
		Log4gUtil.getLog().info("uploadPictures-进入-userid:"+userid+"-token:"+token+"-:"+j.toString());
	}
	
	/**
	 * 
	 * //删除图片  >Nf020
	 */
	public void deleteUserImg(){
		Log4gUtil.getLog().info("deleteUserImg-进入-userid:"+userid+"-token:"+token);
		if(params!=null&&!params.equals("")){
			JSONObject dataJson = DataUtil.dataParser(params);
			if(dataJson.has("imgid"))
				imgid=dataJson.getInt("imgid");
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		net.sf.json.JSONObject j =DataUtil.sendJson();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			Userimg ui = userimgService.selectUserImg(imgid);//id为用户图片Userimg.id
			String path = IPUtil.getFilePath()+"/img/uersImg/picturesImg";
			File f= new File(path, ui.getImgName());
			boolean d = f.delete();
			if(d){
				userimgService.deleteUserImg(ui);
				j.put("code", 1);
			}else{
				j.put("code", -1);
			}
			out.print(j);
		} catch (Exception e) {
			j.put("code", -1);
			out.print(j);
			e.printStackTrace();
		}
		Log4gUtil.getLog().info("deleteUserImg-进入-userid:"+userid+"-token:"+token+"-:"+j.toString());
	}
	
	
	/**
	 * 
	 * //用户图片路劲  >Nf019
	 */
	public void userImgUrl(){
		if(params!=null&&!params.equals("")){
			JSONObject datajson = new JSONObject(params);
			if(datajson.has("userid"))
				userid = datajson.getInt("userid");
		}
		net.sf.json.JSONObject j = DataUtil.sendJson();
		JSONArray json = new JSONArray();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			List<Userimg> uis = userimgService.selectUser(userid);//id为用户Users.id
			if(uis.isEmpty()){//用户没有自己的图片时
				//默认图片项目路径
				String realPath = IPUtil.getFilePath()+"/img/uersImg/picturesImg/MoRenUserImg/";
				File f = new File(realPath);
				//路劲底所有图片
				String[] list = f.list();
				for (int i = 0; i <list.length; i++){
					JSONObject js = new JSONObject();
					js.put("pic",IPUtil.getIp()+"/img/uersImg/picturesImg/MoRenUserImg/"+list[i]);
					json.put(js);
				}
				j.put("code", 1);
				j.put("defualt", 0);
				j.put("imgurl", json.toString());
			}else{//用户有自己的图片时
				for (int i = 0; i < uis.size(); i++) {
					JSONObject js = new JSONObject();
					js.put("id",uis.get(i).getId() );
					js.put("pic", IPUtil.getIp()+"/img/uersImg/picturesImg/"+uis.get(i).getImgName());
					json.put(js);
				}
				j.put("code", 1);
				j.put("defualt",1);
				j.put("imgurl", json.toString());
			}
			out.print(j);
		} catch (Exception e) {
			j.put("code", -1);
			j.put("imgurl", json.toString());
			out.print(j);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 用户头像路径  >Nf018
	 */
	public void headImgUrl(){
		if(params!=null&&!params.equals("")){
		JSONObject datajson = DataUtil.dataParser(params);
		if(datajson.has("userid"))
			userid = datajson.getInt("userid");
		}
		System.out.println("头像");
		HttpServletResponse response = ServletActionContext.getResponse();
		net.sf.json.JSONObject j = DataUtil.sendJson();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			//查询用户信息
			Users u = loginService.selectUserId(userid);//id为用户Users.id
			if(u.getHeadImg()!=null&&!u.getHeadImg().equals("/MoRenHead/")){
				j.put("code", 1);
				j.put("defualt", 1);
				j.put("headurl",IPUtil.getIp()+"/img/uersImg/headImg/"+u.getHeadImg());
				j.put("nickname", u.getNickname());
			}else{
				String realPath = IPUtil.getFilePath()+"/img/uersImg/headImg/MoRenHead/";
				File f = new File(realPath);
				//路劲底所有图片
				String[] list = f.list();
				j.put("code", 1);
				j.put("defualt", 0);
				j.put("headurl",IPUtil.getIp()+"/img/uersImg/headImg/MoRenHead/"+list[0]);
				j.put("nickname", u.getNickname());
			}
			out.print(j);
		} catch (Exception e) {
			j.put("code", -1);
			out.print(j);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 添加反馈  >Nf028
	 */
	public void saveFeedback(){
		Log4gUtil.getLog().info("saveFeedback-进入-userid:"+userid+"-token:"+token);
		if(params!=null&&!params.equals("")){
			JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("vertype"))
				vertype = datajson.getInt("vertype");
			if(datajson.has("ver"))
				ver = datajson.getString("ver");
			if(datajson.has("feedbackContent"))
				feedbackContent=datajson.getString("feedbackContent");
			if(datajson.has("userid"))
				userid = datajson.getInt("userid");
			
			if(datajson.has("appType"))
				appType = datajson.getInt("appType");
			if(datajson.has("siteAreaid"))
				siteAreaid = datajson.getInt("siteAreaid");
			
			System.out.println("进入添加反馈------"+feedbackContent+"--"+userid);
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out=null;
		net.sf.json.JSONObject j = DataUtil.sendJson();	
		try {	
			Version version = null;
			out = response.getWriter();
			version = versionService.selectVersion(ver, vertype,appType);
			if(feedbackContent!=null&&!feedbackContent.equals("")&&version!=null){ 
				Users u = new Users();
				u.setId(userid);
				System.out.println(userid);
				//查询到是那个用户反馈
				Feedback f = new Feedback();
				f.setContent(feedbackContent);
				f.setFtime(new Date());
				f.setUid(userid);
				f.setVerId(version.getId());
				f.setAreaid(siteAreaid);
				//添加反馈到数据库
				feedbackService.insertFeedback(f);
				j.put("code", 1);
			}else{
				j.put("code", 0);
			}	
			out.print(j);
		} catch (Exception e){
			j.put("code", -1);
			out.print(j);
			e.printStackTrace();
		}
		Log4gUtil.getLog().info("saveFeedback-进入-userid:"+userid+"-token:"+token+"-:"+j.toString());
	}
	
	
	public void updataUser(){
		Log4gUtil.getLog().info("updataUser-进入-userid:"+userid+"-token:"+token);
		if(params!=null&&!params.equals("")){
			JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("nickname"))
				nickname=datajson.getString("nickname");
			if(datajson.has("userid"))
				userid =datajson.getInt("userid");
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out=null;
		net.sf.json.JSONObject j = DataUtil.sendJson();	
		try {
			out = response.getWriter();
			Users user = loginService.selectUserId(userid);
			if(user!=null){
				user.setNickname(nickname);
				loginService.updateUser(user);
				j.put("code", 1);
			}else{
				j.put("code",-1);
				j.put("msg","用户不存在");
			}
		} catch (Exception e) {
			j.put("code",-1);
			j.put("msg","服务器异常");
			e.printStackTrace();
		}
		out.print(j);
		Log4gUtil.getLog().info("updataUser-进入-userid:"+userid+"-token:"+token+"-:"+j.toString());
	}
	
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public void setUserimgService(UserimgService userimgService) {
		this.userimgService = userimgService;
	}
	
	public void setFile(File file) {
		this.file = file;
	}

	public void setFeedbackContent(String feedbackContent) {
		this.feedbackContent = feedbackContent;
	}
	public String getFeedbackContent() {
		return feedbackContent;
	}
	public void setFeedbackService(FeedbackService feedbackService) {
		this.feedbackService = feedbackService;
	}

	public void setImgid(int imgid) {
		this.imgid = imgid;
	}

	public void setToken(String token) {
		this.token = token;
	}
	public void setVer(String ver) {
		this.ver = ver;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public void setVersionService(VersionService versionService) {
		this.versionService = versionService;
	}

	public void setVertype(int vertype) {
		this.vertype = vertype;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setParams(String params) {
		this.params = params;
	}

	
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	
	
}
